﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using The_LarX;
using LarX_Windows.Serial;
using LarX_Windows.BinaryConversion;
using LarX_Windows.Update;

namespace LarX_Editor
{
    public partial class LanguageMainForm : Form
    {
        public LanguageSerializeData Data;

        public string Place;

        public LanguageMainForm(string[] arg)
        {
            InitializeComponent();

            //string serial = SerialKey.GenerateTokenKeyBasedOnMAC(4);
            //string activ = SerialKey.GenerateActivationKeyBasedOnToken(serial);
            //bool cor = SerialKey.IsValidActivationKey(activ, serial);

            /*BinaryConvert.ToBinaryDirectory("C:\\Lucas", "C:\\wololo.larb");
            AppVersion version = new AppVersion();
            version.UpdateFile = "C:\\wololo.larb";
            version.VERSION = DateTime.Now;
            version.WebUpdateURL = "http://www.largaming.com/larxeditor/wololo.larb";
            version.Save("C:\\update.larup");*/

            //BinaryConvert.ToFiles("C:\\wololo.larb", "C:\\TesteDescompactado");
            //CompressFile.CompressAllFilesInsideDirAndSubDirs("C:\\Lucas", "C:\\comprimido.cab");

            Data = new LanguageSerializeData("pt-br");

            if (arg.Length > 0)
            {
                if (!string.IsNullOrWhiteSpace(arg[0]))
                {
                    try
                    {
                        Place = arg[0];
                        this.Data = LanguageSerializer.Load(arg[0]);

                        SetUp();
                    }
                    catch { }
                }
            }
        }

        public void SetUp()
        {
            LanguangeNameTextBox.Text = Data.Language;

            for (int i = 0; i < Data.Data.Count; i++)
            {
                WordsList.Items.Add(Data.Data[i].Word);
                KeysList.Items.Add(Data.Data[i].Key);
            }
        }

        public void LoadFile(string place)
        {
            KeysList.Items.Clear();
            WordsList.Items.Clear();

            KeyChangeBox.Text = String.Empty;
            WordBox.Text = String.Empty;

            Place = place;
            this.Data = LanguageSerializer.Load(place);

            SetUp();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Inicia um OpenFileDialog, para o usuário abri um arquivo de Lingua
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Language Files|*.larlang";
            openFileDialog1.Title = "Select a Lar Language File";

            // Se o usuário clicou em OK
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                KeysList.Items.Clear();
                WordsList.Items.Clear();

                KeyChangeBox.Text = String.Empty;
                WordBox.Text = String.Empty;

                Place = openFileDialog1.FileName;
                this.Data = LanguageSerializer.Load(openFileDialog1.FileName);

                SetUp();
            }
        }
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Place == null)
            {
                // Inicia um SaveFileDialog, para o usuário salvar o arquivo de Lingua
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Language Files|*.larlang";
                saveFileDialog.Title = "Find the Place";

                // Se o usuário clicou em OK
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    Data = new LanguageSerializeData(LanguangeNameTextBox.Text);

                    for (int i = 0; i < KeysList.Items.Count; i++)
                    {
                        Data.AddData((int)KeysList.Items[i], (string)WordsList.Items[i]);
                    }
                    LanguageSerializer.Info = Data;

                    string s = saveFileDialog.FileName;
                    if (!s.EndsWith(".larlang"))
                    {
                        s += ".larlang";
                    }
                    LanguageSerializer.Save(s);

                    Place = s;
                }
            }
            else
            {
                Data = new LanguageSerializeData(LanguangeNameTextBox.Text);

                for (int i = 0; i < KeysList.Items.Count; i++)
                {
                    Data.AddData((int)KeysList.Items[i], (string)WordsList.Items[i]);
                }
                LanguageSerializer.Info = Data;
                LanguageSerializer.Save(Place);
            }
        }
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Inicia um SaveFileDialog, para o usuário salvar o arquivo de Lingua
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Filter = "Language Files|*.larlang";
            saveFileDialog.Title = "Find the Place";

            // Se o usuário clicou em OK
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                Data = new LanguageSerializeData(LanguangeNameTextBox.Text);

                for (int i = 0; i < KeysList.Items.Count; i++)
                {
                    Data.AddData((int)KeysList.Items[i], (string)WordsList.Items[i]);
                }

                LanguageSerializer.Info = Data;
                
                string s = saveFileDialog.FileName;
                if (!s.EndsWith(".larlang"))
                {
                    s += ".larlang";
                }
                LanguageSerializer.Save(s);

                Place = s;
            }
        }     

        private void KeysList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (KeysList.SelectedIndex >= 0)
            {
                KeyChangeBox.Text = KeysList.Items[KeysList.SelectedIndex].ToString();
                WordBox.Text = (string)WordsList.Items[KeysList.SelectedIndex];
                WordsList.SelectedIndex = KeysList.SelectedIndex;
            }
        }

        private void WordsList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (WordsList.SelectedIndex >= 0)
            {
                KeyChangeBox.Text = KeysList.Items[WordsList.SelectedIndex].ToString();
                WordBox.Text = (string)WordsList.Items[WordsList.SelectedIndex];
                KeysList.SelectedIndex = WordsList.SelectedIndex;
            }
        }

        private void AcceptButton_Click(object sender, EventArgs e)
        {
            if (KeysList.SelectedItem != null)
            {
                int s = KeysList.SelectedIndex;

                KeysList.Items.Remove(KeysList.SelectedItem);
                KeysList.Items.Insert(s, int.Parse(KeyChangeBox.Text));

                WordsList.Items.Remove(WordsList.SelectedItem);
                WordsList.Items.Insert(s, WordBox.Text);
            }
        }
        private void RemoveButton_Click(object sender, EventArgs e)
        {
            if (KeysList.SelectedItem != null)
            {
                KeysList.Items.Remove(KeysList.SelectedItem);
                WordsList.Items.Remove(WordsList.SelectedItem);
            }
        }
        private void CreateNewButton_Click(object sender, EventArgs e)
        {
            KeysList.Items.Add(KeysList.Items.Count + 1);
            WordsList.Items.Add("Word Empty");
        }


        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Data = new LanguageSerializeData("pt-br");

            KeysList.Items.Clear();
            WordsList.Items.Clear();

            Place = String.Empty;

            KeyChangeBox.Text = String.Empty;
            WordBox.Text = String.Empty;
        }
    }
}
